//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tmf\fn_execC.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

if ((isNil QGVAR(INITIALIZED)) || {!GVAR(INITIALIZED)}) exitWith {
	// Case when the framework is not initialized yet
	DEBUG_1("Delaying execution");
	_this spawn {
		waituntil{GVAR(INITIALIZED) && (time > 6)};
		_this call FFUNC(execC);
	};
};

if (!isDedicated) then {
	private ["_params","_script","_i","_is_fnc","_fnc","_target"];
	_params = [];
	_script = "";
	_target = "";
	if (IS_STRING(_this)) then {
		_script = _this;
	} else {
		if (IS_ARRAY(_this) && {count _this > 0}) then {
			_script = _this select 0;
			if (count _this > 1) then {
				_params = _this select 1;
			};
			if (count _this > 2) then {
				_target = _this select 2;
			};
		};
	};
	if (IS_STRING(_target) && {(_target == "" || _target == getplayerUid player)}) then {
		if (_script != "") then {
			_is_fnc = !(isNil _script);
			DEBUG_1("Client Execute",_is_fnc);
			if (_is_fnc) then {
				_fnc = (missionNameSpace getVariable [_script,""]);
				if (IS_STRING(_fnc)) then {
					_fnc = call compile _script;
				};
				_params spawn _fnc;
			} else {
				_params execVM _script;
			};
		};
	};
};
TRACE("End");
TMFPROFILERSTOP;